#include <bits/stdc++.h>
using namespace std;

vector<int> arr;

int find(int x)
{
    if (arr[x] == x) return x;

    return arr[x] = find(arr[x]);
}

void join(int a, int b)
{
    int pa = find(a);
    int pb = find(b);
    if (pa != pb) arr[pa] = pb;
}

int main()
{
    int m, n, k;
    cin >> m >> n;
    arr.resize(m * n + 1, 0);
    for (int i = 1; i < arr.size(); i++) arr[i] = i;
    cin >> k;
    while (k--)
    {
        int a, b;
        cin >> a >> b;
        join(a, b);
    }
    int ret = 0;
    for (int i = 1; i < arr.size(); i++) {
        if (arr[i] == i) ret++;
    }
    cout << ret << endl;
    return 0;
}